package com.ey12.db.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmManager;

/**
 * Data access object (DAO) for domain model class GmManager.
 * 
 * @see com.ey12.db.domain.GmManager
 * @author MyEclipse Persistence Tools
 */

public class GmManagerDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmManagerDAO.class);
	// property constants
	public static final String TOTAL_ORDER = "totalOrder";
	public static final String WAIT_ORDER = "waitOrder";
	public static final String IS_FREE = "isFree";
	public static final String USERNAME = "username";
	public static final String USERTYPE = "usertype";
	public static final String REALNAME = "realname";


	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmManager transientInstance) {
		log.debug("saving GmManager instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmManager persistentInstance) {
		log.debug("deleting GmManager instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmManager findById(java.lang.Integer id) {
		log.debug("getting GmManager instance with id: " + id);
		try {
			GmManager instance = (GmManager) getHibernateTemplate().get(
					"com.ey12.db.domain.GmManager", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmManager instance) {
		log.debug("finding GmManager instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmManager instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmManager as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByUserName(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByUserType(Object usertype) {
		return findByProperty(USERTYPE, usertype);
	}

	public List findByRealname(Object realname) {
		return findByProperty(REALNAME, realname);
	}

	public List findByTotalOrder(Object totalOrder) {
		return findByProperty(TOTAL_ORDER, totalOrder);
	}

	public List findByWaitOrder(Object waitOrder) {
		return findByProperty(WAIT_ORDER, waitOrder);
	}

	public List findByIsFree(Object isFree) {
		return findByProperty(IS_FREE, isFree);
	}

	public List findAll() {
		log.debug("finding all GmManager instances");
		try {
			String queryString = "from GmManager";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public GmManager merge(GmManager detachedInstance) {
		log.debug("merging GmManager instance");
		try {
			GmManager result = (GmManager) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmManager instance) {
		log.debug("attaching dirty GmManager instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmManager instance) {
		log.debug("attaching clean GmManager instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmManagerDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmManagerDAO) ctx.getBean("GmManagerDAO");
	}
}